package com.example.ticket.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ticket.bean.ResponseBean;
import com.example.ticket.constants.SortBy;
import com.example.ticket.domain.PageDomain;
import com.example.ticket.entity.Authority;
import com.example.ticket.entity.Ban;
import com.example.ticket.entity.User;
import com.example.ticket.entity.UserAuthority;
import com.example.ticket.mapper.AuthorityMapper;
import com.example.ticket.mapper.BanMapper;
import com.example.ticket.service.BanService;
import com.example.ticket.service.UserService;
import com.example.ticket.service.MailService;
import com.example.ticket.utils.UUIDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @Autowired
    BanService banService;

    @Autowired
    AuthorityMapper authorityMapper;

    @Autowired
    BanMapper banMapper;

    @Autowired
    MailService mailService;
/*    @GetMapping("/userlist")
    public ResponseBean find(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<User> list=userService.Userlist();
        if(list!=null){
            PageInfo<User> userinfo = new PageInfo<User>(list);
        return ResponseBean.success(userinfo);
            *//*return list;*//*
        }else{
            return ResponseBean.notFound();
           *//* return list;*//*
        }
    }*/
    @GetMapping("/userlist")
    public ResponseBean page(
            @RequestParam(defaultValue = "5")int pageSize,
            @RequestParam(defaultValue = "1")int pageNum) {
        System.out.println("分页请求"+pageNum+";"+pageSize);
        PageDomain pageDomain=new PageDomain(pageNum,pageSize,"id", SortBy.ASC);
        PageInfo<User> pageInfo=userService.page(pageDomain);
        return ResponseBean.success(pageInfo);
    }

    @GetMapping("/searchUser")
    public ResponseBean SearchUser(String username,
                                   @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,7);
        List<User> list=userService.findByUsername(username);
        if(list!=null){
            PageInfo<User> SearchList = new PageInfo<User>(list);
        return ResponseBean.success(SearchList);
        }else{
            return ResponseBean.notFound();
        }
    }

    @GetMapping("/banlist")
    public ResponseBean BanList(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,7);
        List<Ban> list=banService.Banlist();
        if(list!=null){
            PageInfo<Ban> baninfo = new PageInfo<Ban>(list);
            return ResponseBean.success(baninfo);
        }else{
            return ResponseBean.notFound();
        }
    }

    @GetMapping("/view/{id}")
    public ResponseBean selectById(@PathVariable Integer id){

        User user=userService.selectById(id);
        if (user == null) {
            return ResponseBean.notFound();
        } else {
            return ResponseBean.success(user);
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseBean deleteById(@PathVariable Integer id){
        if (userService.deleteById(id) != 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }

    @PostMapping("/getremark")
    public ResponseBean GetRemark(Integer id,String remark){
        banMapper.updateRemark(id,remark);
        return ResponseBean.success();
    }

    @GetMapping("/sendEmail")
    public ResponseBean SendEmail(String email){
        User user=userService.checkEmail(email);
        if(userService.checkEmail(email)==null){
            return ResponseBean.notFound();
        }else{
        String subject = "来自408票据管理系统的重置密码";
        String context = "<div><h1>欢迎使用408票据管理系统</h1></div>" +
                "<div>您的用户名是"+user.getUsername()+"</div>" +
                "<div>点击下方重置您的账号密码</div>" +
                "<a href=\"http://120.77.150.238:8089/resetPassword?username="+user.getUsername()+"\">前往重置密码"+"</a>" +
                "<div>若有疑问，请联系客服-电话12345678</div>";
        mailService.sendHtmlMail(email,subject,context);
        return ResponseBean.success();
        }
    }

    @PostMapping("/freeze/{id}")
    public ResponseBean freezeById(@PathVariable Integer id){
        User user=userService.selectById(id);
        user.setStatus(0);
        user.setId(id);

        if (userService.freezeById(user) != null) {
            Ban ban=new Ban();
            ban.setUserId(id);
            ban.setRemark("");
            banService.insertBan(ban);
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }

    @PostMapping("/activate/{id}")
    public ResponseBean activateById(@PathVariable Integer id){
        User user=userService.selectById(id);
        user.setStatus(1);
        user.setId(id);
        if (userService.freezeById(user) != null) {
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }


    @PostMapping("/update")
    public ResponseBean update(@RequestBody User user){
        User user1=userService.selectById(user.getId());
        user.setCreated(user1.getCreated());
        user.setStatus(user1.getStatus());
        if (userService.updateUser(user) != null) {
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }

    @PostMapping("/addUser")
    public ResponseBean addUser(@RequestBody User user){
        String code = UUIDUtils.getUUID()+ UUIDUtils.getUUID();
        user.setCode(code);
        User user1=userService.insertUser(user);
        User user2=userService.selectByUsername(user1.getUsername());
        System.out.println(user1.getUsername());
        if (user1!= null) {
            System.out.println(user2.getId());
            userService.insertUserAuthority(user2.getId());
            return ResponseBean.success();
        } else {
            return ResponseBean.fail();
        }
    }


    @GetMapping("/authoritylist")
    public ResponseBean AuthorityList(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,7);
        List<UserAuthority> list=authorityMapper.AuthorityList();
        if(list!=null){
            PageInfo<UserAuthority> authoritylist = new PageInfo<UserAuthority>(list);

            return ResponseBean.success(authoritylist);
        }else{
            return ResponseBean.notFound();
        }
    }

    @PostMapping("/upauthority/{id}")
    public ResponseBean UpAuthority(@PathVariable Integer id){
        int res=authorityMapper.ChangeAuthority(id);
        if(res==1) {
            return ResponseBean.success();
        }else{
            return ResponseBean.fail();
        }
    }
}
